package com.springboot.demo.dp;

import org.junit.Test;

/**
 * 动态规划demo
 *
 * @author supu
 * @date 2019-12-12 15:34
 **/
public class DpDemo {
    private int[] a = {6,2,66,10,0,23,8};

    @Test
    public void getMaxNumberTest(){
        System.out.println("最大值：" + getMaxNumber(a, a.length-1));
    }

    @Test
    public void getSumTest(){
        System.out.println("总和：" + getSum(a, a.length-1));
    }

    @Test
    public void getEvenCountTest(){
        System.out.println("偶数个数：" + getEvenCount(a, a.length-1));
    }

    @Test
    public void getGapSumTest(){
        System.out.println("不连续数最大和：" + getGapSum(a, a.length-1));
    }

    public static int getMaxNumber(int[] a, int i) {
        return i == 0 ? a[i] : Math.max(getMaxNumber(a, i - 1), a[i]);
    }

    public static int getSum(int[] a, int i) {
        return i == 0 ? a[i] : Math.addExact(getSum(a, i - 1), a[i]);
    }

    public static int getEvenCount(int[] a, int i) {
        return i == 0 ? isEven(a[i]) : getEvenCount(a, i - 1) + isEven(a[i]);
    }

    private static int isEven(int i) {
        return (i & 1) != 1 ? 1 : 0;
    }

    public static int getGapSum(int[] a, int i) {
        return i <= 0 ? a[0] : (i == 1 ? Math.max(a[i],a[i-1]) : Math.max(a[i] + getGapSum(a, i - 2), getGapSum(a, i - 1)));
    }
}
